<template>
  <div class="fontDa">
    <el-dialog :visible.sync="dialogVisible" fullscreen destroy-on-close append-to-body @close="cacel()"
               :before-close="handleClose">
      <el-container>
        <el-main :style="mode=='hz'?'height:calc(100vh - 210px); padding: 0 45px;':'height:calc(100vh - 130px); padding: 0 45px;'">
          <div v-if="mode=='df'">
            <cpXp ref="cpXp"></cpXp>
          </div>
          <div v-if="mode=='hz'">
            <cpHz ref="cpHz" @openXp="openXp"></cpHz>
          </div>
        </el-main>
      </el-container>
      <div class="fontDa">
<!--      <div class="el-icon-back arrowBtn" @click="up()" v-if="dqwz>0&&mode=='df'"></div>-->
<!--      <div class="el-icon-right arrowBtn" @click="next()" v-if="mode!='hz'"></div>-->
        <div class="el-icon-back arrowBtn" @click="up()" v-if="currentPage!=1"></div>
        <div class="el-icon-right arrowBtn" @click="next()" v-if="mode!='hz'"></div>
      </div>
      <div class="fontDa">
<!--      <div class="shuoMing" style="margin-left: 140px;" v-if="mode=='hz'">★点击“返回测评界面”按钮，可返回该张测评票进行测评分值修改。</div>-->
        <div class="flex justifyContentSpaceBetween font22" style="margin-left: 40px; margin-top:20px;" v-if="mode=='hz'">
          <div><!--★点击“返回测评界面”按钮，可返回该张测评票进行测评分值修改。--></div>
          <div class="flex justifyContentSpaceBetween " style="margin-right:60px;">
            <span><img src="/img/logo.png" width="40" height="40"></span><span class="ml10 font28">共享运营公司测评系统</span>
          </div>
        </div>
      <div class="buttomBtn">
        <!--      <span class="nextBtn">-->
        <!--        <el-button  type="primary">返回</el-button>-->
        <!--      </span>-->
        <span slot="footer" class="dialog-footer" style="position: relative;z-index: 2">
          <el-button type="primary" @click="up()" v-if="currentPage!=1" class="nextBtn">{{'上一张选票'}}</el-button>
<!--        <el-button type="primary" @click="up()" v-if="dqwz>0||mode=='hz'" class="nextBtn">{{mode=='hz'?'返回':'上一张选票'}}</el-button>-->
        <!-- <span v-if="xpList.length>=1&&dqwz<xpList.length&&mode!='hz'" style="float: left">评票：{{ dqwz + 1 }}/{{ xpList.length }}</span> -->
        <el-tooltip content="点击可查看所有测评对象及测评结果。" placement="top" effect="light" v-if="dqwz<xpList.length&&mode!='hz'">
          <el-button type="warning" @click="openHz()" class="nextBtn">测评结果查询</el-button>
        </el-tooltip>
        <el-button type="primary" v-if="mode=='hz'" @click="tiJiao()" class="nextBtn">提交测评</el-button>
<!--        <el-button type="primary" @click="next()" v-if="mode!='hz'" class="nextBtn">下一张选票</el-button>-->
<!--        <el-button type="primary" v-if="mode!='hz'&&xpList.length==1&&isNotLastTab" @click="nextTab" class="nextBtn">下一步</el-button>-->
        <el-button type="primary" v-if="mode!='hz'" @click="nextTab" class="nextBtn">下一张选票</el-button>

<!--          <span v-if="mode!='hz'" class="ml30 yeMa">-->
<!--             {{dqwz+1}} / {{xpList.length}}-->
<!--            </span>-->
      </span>
        <span v-if="mode!='hz'" class="yeMa">
          <span class="blue"> 当前第 {{currentPage}} 页</span> / 共 {{allPageData.length}} 页
<!--             <span class="blue"> 当前第 {{dqwz+1}} 页</span> / 共 {{xpList.length}} 页-->
            </span>
      </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {wdXuanPiao, wdXuanPiaoXp, tiJiaoHd, tiJiaoXpCode} from "@/api/ceping/cepinghdglxpxxb";
import {currentPageSave,getUnPickTicket} from "@/api/ceping/jxevalvote";
import {onlineRegister,onlineVerification} from "@/api/admin/online";
import cpXp from "./cpIndexXp";
import cpHz from "./cpIndexHz";

export default {
  name: "cpIndex.vue",
  components: {
    cpXp,
    cpHz
  },
  data() {
    return {
      type: "",
      hdId: null,
      xcCode: "",
      dialogVisible: false,
      xpList: [],//所有选票
      dqwz: 0,//当前位置
      mode: "df",//df或hz
      timer: null,
      ishaveLast:false,
      isNotLastTab:false,
      sheetType:null,
      allPageData:[],
      doneList:[],
      currentPage:0//当前页
    }
  },
  beforeDestroy() {
    clearInterval(this.timer);
  },
  methods: {
    init() {
      this.xpList = [];
      this.dqwz = 0;
      this.mode = "df";
    },
    cacel() {
      //退出全屏
      function exitFullscreen() {
        if (document.exitFullscreen) {
          document.exitFullscreen();
        } else if (document.msExitFullscreen) {
          document.msExitFullscreen();
        } else if (document.mozCancelFullScreen) {
          document.mozCancelFullScreen();
        } else if (document.webkitExitFullscreen) {
          document.webkitExitFullscreen();
        }
      }

      clearInterval(this.timer);
      exitFullscreen();
      this.$emit('next');
    },
    //初始按会议
    initHd(hdId) {
      this.init();
      this.type = "HD";
      this.hdId = hdId;
      wdXuanPiao(hdId).then(res => {
        if (res.data.data.length > 0) {
          function fullScreen() {
            var element = document.documentElement;
            if (element.requestFullscreen) {
              element.requestFullscreen();
            } else if (element.msRequestFullscreen) {
              element.msRequestFullscreen();
            } else if (element.mozRequestFullScreen) {
              element.mozRequestFullScreen();
            } else if (element.webkitRequestFullscreen) {
              element.webkitRequestFullscreen();
            }
          }
          fullScreen();
          this.dialogVisible = true;
          this.xpList = res.data.data;
          this.open(0)
        } else {
          this.$notify.closeAll();
          this.$notify.error('会议中没有选票信息')
        }
      })
    },
    //初始按选票
    initXp(dataObj) {
      debugger
      onlineVerification(dataObj.xpCode).then(ress=>{
        if(ress.data.data=="false"||dataObj.ishaveLast){
          this.init();
          this.type = "XP";
          this.ishaveLast=dataObj.ishaveLast;
          this.xcCode = dataObj.xpCode;
          this.isNotLastTab=dataObj.isNotLastTab;
          this.sheetType=dataObj.sheetType;
          this.allPageData=dataObj.allPageData;
          this.doneList=dataObj.doneList;
          this.currentPage=dataObj.currentPage;
          wdXuanPiaoXp(this.xcCode).then(res => {
            if (res.data.data.length > 0) {
              function fullScreen() {
                var element = document.documentElement;
                if (element.requestFullscreen) {
                  element.requestFullscreen();
                } else if (element.msRequestFullscreen) {
                  element.msRequestFullscreen();
                } else if (element.mozRequestFullScreen) {
                  element.mozRequestFullScreen();
                } else if (element.webkitRequestFullscreen) {
                  element.webkitRequestFullscreen();
                }
              }
              this.timer = setInterval(()=>{
                onlineRegister(this.xcCode).then(resss=>{});
              }, 30000);
              fullScreen();
              this.dialogVisible = true;
              this.xpList = res.data.data;
              this.openXp(dataObj.currentPageObj.voteId)
              //this.open(0)
            } else {
              this.$notify.closeAll();
              this.$notify.error('票号中没有选票信息');
            }
          })
        }else{
          this.$notify.closeAll();
          this.$notify.error('票号正在打分，不可重复上线');
        }
      })
    },
    //判断是否测评完成
    yzDf(){
      let yzDf = 0;
      yzDf=this.$refs.cpXp.yzDf();
      return yzDf;
    },
    up() {
      if (this.mode == "hz") {
        this.mode = "df";
        //this.dqwz = this.xpList.length - 1;
        this.open(this.dqwz)
      } else {
        let yzDf = this.$refs.cpXp.yzDf();
        if (yzDf > 0) {
          this.$confirm('当前页还有“' + (yzDf == 1 ? '指标项' : '评价等次') + '”没有测评，请继续完成测评。', '提示', {
            confirmButtonText: '执行操作',
            showConfirmButton: false,
            cancelButtonText: '继续打分',
            type: 'warning'
          }).then(() => {
            this.currentPageSave('upTab')
            // if (this.dqwz == 0) {
            //   this.mode = "hz";
            //   this.openHz()
            // } else {
            //   this.dqwz = this.dqwz - 1;
            //   this.open(this.dqwz)
            // }
          }).catch(() => {
          })
        } else {
          this.currentPageSave('upTab')
          // if (this.dqwz == 0) {
          //   this.mode = "hz";
          //   this.openHz()
          // } else {
          //   this.dqwz = this.dqwz - 1;
          //   this.open(this.dqwz)
          // }
        }
      }
    },
    next() {
      if (this.mode == "hz") {
        this.mode = "df";
        this.dqwz = 0;
        this.open(this.dqwz)
      } else {
        let yzDf = this.$refs.cpXp.yzDf();
        if (yzDf > 0) {
          this.$confirm('当前页还有“' + (yzDf == 1 ? '指标项' : '评价等次') + '”没有测评，请继续完成测评。', '提示', {
            confirmButtonText: '执行操作',
            showConfirmButton: false,
            cancelButtonText: '继续打分',
            type: 'warning'
          }).then(() => {
            this.currentPageSave('nextTab')
            // if (this.dqwz == this.xpList.length - 1) {
            //   this.currentPageSave('nextTab')
            //   //this.$emit('nextTab','ceping');
            //   // this.mode = "hz";
            //   // this.openHz()
            // } else {
            //   this.currentPageSave('nextPage')
            //   this.dqwz = this.dqwz + 1;
            //   this.open(this.dqwz)
            // }
          }).catch(() => {
          })
        } else {
          this.currentPageSave('nextTab')
          // if (this.dqwz == this.xpList.length - 1) {
          //   this.currentPageSave('nextTab')
          //   //this.$emit('nextTab','ceping');
          //   //判断是不是最后一个页签，后面是否还有票据，如果后面还有自动跳到下一步骤，如果是最后一步，执行原来的代码
          //   // if(this.isNotLastTab){
          //   //   this.$emit('nextTab','ceping');
          //   //   return false;
          //   // }else{
          //   //   this.mode = "hz";
          //   //   this.openHz()
          //   // }
          // } else {
          //   this.currentPageSave('nextPage')
          //   this.dqwz = this.dqwz + 1;
          //   this.open(this.dqwz)
          // }
        }
      }
    },
    //下一个步骤
    nextTab(){
      this.next();
      //this.$emit('nextTab','ceping');
    },
    //原来的openXp
    openXp(id) {
      if (id) {
        for (var i in this.xpList) {
          if (this.xpList[i].id == id) {
            this.dqwz = i * 1;
            this.open(this.dqwz);
          }
        }
      } else {
        this.open(0);
      }
    },
    openHzToXp1(id,xcCode){
      this.xcCode=xcCode;
      wdXuanPiaoXp(xcCode).then(res => {
        this.xpList = res.data.data;
        this.openXp(id)
      })

    },

    //从汇总页来的
    openHzToXp(dataObj){
      this.xcCode=dataObj.xcCode;
      this.allPageData=dataObj.allPageData;
      this.doneList=dataObj.doneList;
      this.currentPage=dataObj.currentPage;
      this.xcCode=dataObj.xcCode;
      wdXuanPiaoXp(this.xcCode).then(res => {
        this.xpList = res.data.data;
        this.openXp(dataObj.currentPageObj.voteId)
      })
    },

    //原来的open
    open(i) {
      this.mode = "df";
      this.$nextTick(() => {
        this.$refs.cpXp.initXp(this.xpList[i].id)
      })
    },
    openHz() {
      if (this.mode == "hz") {
        // var param={
        //   lastSheetType:
        // }
        this.currentPageSave('toHuiZong')
        //this.$emit('toHuiZong','ceping');
        //汇总页面调到新的页面，下面这是原来得数据，先注释
        // this.$nextTick(() => {
        //   if (this.type == "HD") {
        //     this.$refs.cpHz.initHd(this.hdId)
        //   } else {
        //     this.$refs.cpHz.initXp(this.xcCode)
        //   }
        // })
        // return;
      }
      let yzDf = this.$refs.cpXp.yzDf();
      if (yzDf > 0) {
        this.$confirm('当前页还有“' + (yzDf == 1 ? '指标项' : '评价等次') + '”没有测评，请继续完成测评。', '提示', {
          confirmButtonText: '执行操作',
          showConfirmButton: false,
          cancelButtonText: '继续打分',
          type: 'warning'
        }).then(() => {
          this.mode = "hz";
          this.$nextTick(() => {
            this.currentPageSave('toHuiZong')
           // this.$emit('toHuiZong','ceping');
            //原来的代码，先注释
            // if (this.type == "HD") {
            //   this.$refs.cpHz.initHd(this.hdId)
            // } else {
            //   this.$refs.cpHz.initXp(this.xcCode)
            // }
          })
        }).catch(() => {
        })
      } else {
        this.mode = "hz";
        this.$nextTick(() => {
          this.currentPageSave('toHuiZong')
         // this.$emit('toHuiZong','ceping');
          //原来的代码，先注释
          // if (this.type == "HD") {
          //   this.$refs.cpHz.initHd(this.hdId)
          // } else {
          //   this.$refs.cpHz.initXp(this.xcCode)
          // }
        })
      }
    },
    tiJiao() {
      this.$confirm('正式提交后将不可修改，是否确认提交测评？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        if (this.type == "HD") {
          tiJiaoHd(this.hdId).then(res => {
            if (res.data.code == "0") {
              this.$notify.success("此次测评会议已提交成功");
              this.cacel();
              this.dialogVisible = false;
            } else {
              this.$notify.closeAll();
              this.$notify.error(res.data.msg)
            }
          })
        } else {
          tiJiaoXpCode(this.xpList[0].hdId, this.xcCode).then(res => {
            if (res.data.code == "0") {
              this.$notify.success("此次测评会议已提交成功");
              this.dialogVisible = false;
              this.cacel();
            } else {
              this.$notify.closeAll();
              this.$notify.error(res.data.msg)
            }
          })
        }
      })
    },
    handleClose(done) {
      this.$confirm('您的测评还没有完成，请继续完成测评。', "提示", {
        confirmButtonText: '执行操作',
        showConfirmButton: false,
        cancelButtonText: '继续打分',
        type: 'warning'
      }).then(_ => {
        done();
      }).catch(_ => {
      });
    },
    //去汇总页之前需要先保存数据
    currentPageSave(toPage){
      const param={
        voteId:this.xpList[this.dqwz].id,
        sheetType:1,
        xpCode:this.xcCode,
       cppId:0
      }
      currentPageSave(param).then(res=>{
        if(res.data.code==0){
          if(toPage=='toHuiZong'){
            var params={
              sheetType:1,
              clickTab:'ceping',
              xpid:this.xpList[this.dqwz].id,
              cppId:this.xpList[this.dqwz].cppId,
              currentPageObj:{
                clickTab:'ceping',
                sheetType:1,
              }
            }
            this.$emit('toHuiZong',params);
            //this.$emit('toHuiZong','ceping');
          }else if(toPage=='nextTab'){
            var params={
              toPage:'ceping',
              tpXpId:this.xpList[this.dqwz].id,
              cppId:this.xpList[this.dqwz].cppId
            }
            this.$emit('nextTab',params);
          }else if(toPage=='upTab'){
            this.$emit('lastTab','ceping');
          }
        }
      })
    }
  }
}
</script>

<style scoped>
</style>
